import { useRequest } from 'umi';
import { useCallback, useMemo, useState } from 'react';

import { getTenantID, requestFunComponFiex } from '@/utils';
import { loadMdicMap } from '@/services/BaseLayout';

import { BidevaluaManageAPI } from '../common/type';
import { ManageSearchColumns, ManageTabSwitch } from '../mconfig';
import { searchCriteriaFun } from '../../Acommon';
import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
const baseUrl = getBaseUrl();

const ManageServices = (curRes: Record<string, any>) => {
  /**
   * 获取数据字典
   * */
  const loadMdicIds: string[] = [`${getTenantID()}${ILoadMapTypeAll.EXPERT_BUSINESSTYPE}`];

  /**
   * 获取数据字典loadMdicMap
   * */
  const loadMdicMapList = useRequest(() => {
    return loadMdicMap(loadMdicIds);
  });

  /**
   * 保存当前的搜索参数
   * */
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /**
   * 管理页获取数据方法
   * */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (reqParams: BidevaluaManageAPI.IAssetTableRequestType) => {
      let PostParams: Record<string, any> = {
        current: 1,
        pageSize: 10,
        dicTypeIds: loadMdicIds?.toString(),
        sort: 'createTime', //必填
        order: 'desc', //必填
        ...ManageTabSwitch(curRes),
        ...reqParams,
      };

      /***保存一份参数**/
      setManageTableParams(PostParams);

      // delete PostParams['createFormName'];
      // delete PostParams['createFormNameUser'];
      // if (PostParams?.createrID && PostParams?.createrID?.length > 0) {
      //   PostParams['createrID'] = PostParams?.createrID?.toString();
      // } else {
      //   delete PostParams['createrID'];
      // }

      // if (PostParams?.createTime) {
      //   const [createTimeStart, createTimeEnd] = PostParams.createTime;
      //   PostParams['createTimeStart'] = createTimeStart.format('YYYY-MM-DD 00:00:00');
      //   PostParams['createTimeEnd'] = createTimeEnd.format('YYYY-MM-DD 23:59:59');
      //   delete PostParams['createTime'];
      // }

      let reqUrl = `${baseUrl}/purchaseSourceExpertController/dataRepresentativeGrid/${PostParams?.params}`;

      PostParams = searchCriteriaFun(PostParams);

      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**
   * 重新请求的方法
   * */
  const reloadRequest = useCallback((reqParams: BidevaluaManageAPI.IAssetTableRequestType | any) => {
    reqParams = searchCriteriaFun(reqParams);
    run({ ...reqParams });
  }, []);

  /**
   * 页面数据及分页信息
   * */
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /**
   * 搜索功能配置columns
   * */
  const searchList = useMemo(() => {
    return {
      loadMdicMapList,
      searchList: ManageSearchColumns(loadMdicMapList) as any,
    };
  }, [loadMdicMapList, manageTableParams]);

  return {
    loading,
    reloadRequest,
    manageTableParams,
    manageReload: refresh,
    ...tableValues, //页面列表值
    ...searchList,
  };
};

export { ManageServices };
